﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Effects.Wave
{
    /// <summary>
    ///  Class converts index samples between wave files with different sample rates and channels counts. 
    /// </summary>
    /// <remarks>
    ///  <para>
    ///   Class contains method to convert sample indexes pointing at the same time moment between wave files with different headers.
    ///  </para>
    ///  <example>
    ///   <code>
    ///    Wave wav1; // Wave file with 44100  Hz sampling rate and 1 channel
    ///    Wave wa2v; // Wave file with 48000 Hz sampling rate and 2 channel
    ///    int sampleIndex1; // sample index refering to certain point
    ///     
    ///     int sampleIndex2 = SampleToSampleConverter.SampleToSample(sampleIndex1, wav1.FmtChunk.SampleRate, wav1.FmtChunk.Channels, wav2.FmtChunk.SampleRate, wav2.FmtChunk.Channels);
    ///     // sampleIndex2 now refers to point that is equally far off file's beginning as sampleIndex1
    ///   </code>
    ///  </example>
    /// </remarks>
    public class SampleToSampleConverter
    {

        /// <summary>
        ///  Converts sample index from one file to another.
        /// </summary>
        /// <param name="sourceSample"> Sample index in source file </param>
        /// <param name="sourceSampleRate"> Sampling rate of source file </param>
        /// <param name="sourceChannels"> Channels count of source file </param>
        /// <param name="destSampleRate"> Sampling rate of destination file </param>
        /// <param name="destChannels"> Channels count of destination file </param>
        /// <returns> Sample index to the destination file pointing at the same time moment as the given source index </returns>
        /// <remarks>
        ///  <example>
        ///   <code>
        ///    Wave wav1; // Wave file with 44100  Hz sampling rate and 1 channel
        ///    Wave wa2v; // Wave file with 48000 Hz sampling rate and 2 channel
        ///    int sampleIndex1; // sample index refering to certain point
        ///     
        ///     int sampleIndex2 = SampleToSampleConverter.SampleToSample(sampleIndex1, wav1.FmtChunk.SampleRate, wav1.FmtChunk.Channels, wav2.FmtChunk.SampleRate, wav2.FmtChunk.Channels);
        ///     // sampleIndex2 now refers to point that is equally far off file's beginning as sampleIndex1
        ///   </code>
        ///  </example>
        /// </remarks>
        public static int SampleToSample(int sourceSample, int sourceSampleRate, int sourceChannels, int destSampleRate, int destChannels)
        {
            // get sample index in channel
            int channelSample = sourceSample / sourceChannels;

            // get time of the sample in milis
            int sourceMilis = channelSample * 1000 / sourceSampleRate;

            // sample index in destiantion channel
            int destChannelSample = sourceMilis * destSampleRate / 1000;
            return destChannelSample * destChannels;
        }
    }
}
